﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace TBM.Tools
{
    /// <summary>
    /// 图片缩放
    /// </summary>
    public class ImageScaling
    {
        /// <summary>
        /// 源图片
        /// </summary>
        Bitmap originImage;

        /// <summary>
        /// 初始化对象
        /// </summary>
        /// <param name="image">图片流</param>
        public ImageScaling(Stream image)
        {
            originImage = new Bitmap(image);
        }

        /// <summary>
        /// 裁剪至
        /// </summary>
        /// <param name="width">新的宽</param>
        /// <param name="height">新的高</param>
        /// <returns></returns>
        public Bitmap ScaleTo(int width, int height)
        {
            Bitmap img;
            // 改变后的图片宽
            int cWidth = 0; 
            // 改变后的图片高
            int cHeight = 0;
            // 开始坐标X
            int startX = 0;
            // 开始坐标Y
            int startY = 0;

            //cWidth = width;
            //cHeight = (int)Math.Ceiling(((float)width / cWidth) * cHeight);
            
            //if (cHeight < height)
            //{
            //    cWidth = (int)Math.Ceiling(((float)height / cHeight) * cWidth);
            //    cHeight = height;
            //}

            cWidth = originImage.Width;
            cHeight = (int)(cWidth * ((float)height / width));

            if (cHeight > originImage.Height)
            {
                cWidth = (int)Math.Ceiling(((float)originImage.Height / cHeight) * cWidth);
                cHeight = originImage.Height;
            }

            startX = (originImage.Width - cWidth) / 2;
            startY = (originImage.Height - cHeight) / 2;

            img = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(img);
            // 设置高质量插值法
            g.InterpolationMode = InterpolationMode.High;
            // 设置高质量,低速度呈现平滑程度
            g.CompositingQuality = CompositingQuality.HighQuality;
            // 消除锯齿
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.DrawImage(originImage, new Rectangle(0, 0, width, height), new Rectangle(startX, startY, cWidth, cHeight), GraphicsUnit.Pixel);
            g.Dispose();

            return img;
        }

        /// <summary>
        /// 裁剪并保存图片
        /// </summary>
        /// <param name="filePath">保存路径</param>
        /// <param name="width">图片宽</param>
        /// <param name="height">图片高</param>
        /// <param name="format">图片格式</param>
        /// <returns></returns>
        public bool ScaleToSave(string filePath, int width, int height, ImageFormat format)
        {
            try
            {
                Bitmap img = ScaleTo(width, height);
                img.Save(filePath, format);
                img.Dispose();

                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
